import Vue from "vue";
import Router from "vue-router";
import findLast from "lodash/findLast";
import { notification } from "ant-design-vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import NotFound from "./views/404";
import Forbidden from "./views/403";
import { check, isLogin } from "./utils/auth";

Vue.use(Router);

const router = new Router({
  mode: "history",
  base: '/ndTalentView',
  routes: [
    {
      path: "/menu",
      name: "menu",
      meta: { title: "菜单" },
      component: () =>
          import(/* webpackChunkName: "dashboard" */ "./views/talentCooperation/TalentMenu")
    },
    {
      path: "/home",
      name: "home",
      meta: { title: "主页" },
      component: () =>
          import(/* webpackChunkName: "dashboard" */ "./views/talentCooperation/TalentMain")
    },
    {
      path: "/tools",
      name: "tools",
      meta: { title: "工具" },
      component: () =>
        import(/* webpackChunkName: "dashboard" */ "./views/talentCooperation/TalentTools")
    },
    {
      path: "/manage",
      name: "manage",
      meta: { title: "管理" },
      component: () =>
        import(/* webpackChunkName: "dashboard" */ "./views/talentCooperation/TalentManage")
    },
    {
      path: "/statistics",
      name: "statistics",
      meta: { title: "统计" },
      component: () =>
        import(/* webpackChunkName: "dashboard" */ "./views/talentCooperation/TalentStatistics")
    },
    {
      path: "/resume",
      name: "resume",
      meta: { title: "简历" },
      component: () =>
          import(/* webpackChunkName: "dashboard" */ "./views/talentCooperation/TalentResume")
    },
    {
      path: "/403",
      name: "403",
      hideInMenu: true,
      component: Forbidden
    },
    {
      path: "*",
      name: "404",
      hideInMenu: true,
      component: NotFound
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }
  const record = findLast(to.matched, record => record.meta.authority);
  if (record && !check(record.meta.authority)) {
    if (!isLogin() && to.path !== "/user/login") {
      next({
        path: "/user/login"
      });
    } else if (to.path !== "/403") {
      notification.error({
        message: "403",
        description: "你没有权限访问，请联系管理员咨询。"
      });
      next({
        path: "/403"
      });
    }
    NProgress.done();
  }

  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
